package com.flower.controller.buyer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flower.annotation.Log;
import com.flower.domain.dto.buyer.review.ReviewDto;
import com.flower.domain.vo.buyer.ReviewVO;
import com.flower.service.ReviewService;
import com.flower.utils.AjaxResult;
import com.flower.utils.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 购买者端评价信息
 */
@Slf4j
@RestController
@RequestMapping("/buyer/review")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 分页获取用户评价列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public AjaxResult getUserReviews(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<ReviewVO> reviews = reviewService.getUserReviews(page, pageSize);
        return AjaxResult.success(reviews);
    }

    /**
     * 根据商品ID获取商品评价列表
     * @param productId
     * @return
     */
    @GetMapping("/product")
    public AjaxResult getProductReviews(@RequestParam Integer productId) {
        List<ReviewVO> reviews = reviewService.getReviewsByProductId(productId);
        return AjaxResult.success(reviews);
    }

    /**
     * 添加商品评价
     * @param reviewDto
     * @return
     */
    @Log(name = "添加商品评价")
    @PostMapping("/add")
    public AjaxResult addReview(@RequestBody ReviewDto reviewDto) {
        reviewDto.setBuyerId(UserContext.getUserId().intValue());
        reviewService.addReview(reviewDto);
        return AjaxResult.success();
    }
} 